package doveclient

import (
	"encoding/json"
	"github.com/juju/errors"
	"io"
	"io/ioutil"
)

func unmarshalResp(r io.Reader) (map[string]interface{}, error) {
	ret, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}

	var m = make(map[string]interface{})

	err = json.Unmarshal(ret, &m)
	if err != nil {
		return nil, err
	}

	if m["status"].(string) != "ok" {
		return nil, errors.New(string(ret))
	}

	return m, nil
}
